﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Data.Entity;
using System.Linq;
using System.Web;
using System.Web.Mvc;

namespace shop.Areas.admin.Controllers
{
    [Authorize]
    public class sizeController : Controller
    {
        private LongHAEntities db = new LongHAEntities();

        //
        // GET: /admin/size/
        public string checkmasize(string input)
        {
            var data = db.KichThuocs.Where(s => s.MaKT == input).ToList();
            if(data.Count > 0 )
            {
                return "1"; // đã sử dụng 
            }
            if(data.Count ==0 )
            {
                return "0"; // có thể sử dụng 
            }
            return "";
        }
        public ActionResult Index()
        {
            return View(db.KichThuocs.ToList());
        }

        //
        // GET: /admin/size/Details/5

        public ActionResult Details(int id = 0)
        {
            KichThuoc kichthuoc = db.KichThuocs.Find(id);
            if (kichthuoc == null)
            {
                return HttpNotFound();
            }
            return View(kichthuoc);
        }

        //
        // GET: /admin/size/Create

        public ActionResult Create()
        {
            return View();
        }

        //
        // POST: /admin/size/Create

        [HttpPost]
        [ValidateAntiForgeryToken]
        public ActionResult Create(KichThuoc kichthuoc)
        {
            if (ModelState.IsValid)
            {
                db.KichThuocs.Add(kichthuoc);
                db.SaveChanges();
                return RedirectToAction("Index");
            }

            return View(kichthuoc);
        }

        //
        // GET: /admin/size/Edit/5

        public ActionResult Edit(int id = 0)
        {
            KichThuoc kichthuoc = db.KichThuocs.Find(id);
            
            if (kichthuoc == null)
            {
                return HttpNotFound();
            }
            return View(kichthuoc);
        }

        //
        // POST: /admin/size/Edit/5

        [HttpPost]
        [ValidateAntiForgeryToken]
        public ActionResult Edit(KichThuoc kichthuoc)
        {
            if (ModelState.IsValid)
            {
                
                db.Entry(kichthuoc).State = EntityState.Modified;
                db.SaveChanges();


                return RedirectToAction("Index");
            }
            return View(kichthuoc);
        }

        //
        // GET: /admin/size/Delete/5

        //public ActionResult Delete(int id = 0)
        //{
        //    KichThuoc kichthuoc = db.KichThuocs.Find(id);
        //    if (kichthuoc == null)
        //    {
        //        return HttpNotFound();
        //    }
        //    return View(kichthuoc);
        //}

        //
        // POST: /admin/size/Delete/5

        [HttpPost, ActionName("Delete")]
        
        public ActionResult Delete(int id)
        {
            
            KichThuoc kichthuoc = db.KichThuocs.Find(id);
            string sj = kichthuoc.MaKT;          
            try
            {
                db.KichThuocs.Remove(kichthuoc);
                db.SaveChanges();
                var sizelist = db.SanPham_KichThuoc.Where(s => s.MaKT == sj).ToList();
                 foreach(var item in sizelist)
                 {
                     var skgh = this.db.SanPham_KichThuoc.Where(s => s.MaKT == item.MaKT).SingleOrDefault();
                     this.db.SanPham_KichThuoc.Remove(skgh);
                     this.db.SaveChanges();
                 }
                
                return Content(Boolean.TrueString);
            }
            catch
            {
                return Content(Boolean.FalseString);
            }
            
        }

        
        protected override void Dispose(bool disposing)
        {
            db.Dispose();
            base.Dispose(disposing);
        }
    }
}